const fs = require('node:fs')
const path = require('node:path')

// 设置要添加的依赖及其版本
const dependency = '@dev-crafter/eslint-config'
const version = 'workspace:*'

// packages目录的路径
const packagesDir = path.join(__dirname, '../../packages')

// 获取所有的package目录
const packageDirs = fs.readdirSync(packagesDir).filter((file) => {
  return fs.statSync(path.join(packagesDir, file)).isDirectory()
})

// 为每个package添加依赖
packageDirs.forEach((dir) => {
  const packageJsonPath = path.join(packagesDir, dir, 'package.json')
  const packageJson = require(packageJsonPath)

  if (!packageJson.devpendencies)
    packageJson.devpendencies = {}

  // 添加依赖（如果已有该依赖，则更新版本）
  packageJson.devpendencies[dependency] = version

  // 写回package.json文件
  fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
})
